//
//  DHGategoryView.m
//  DHKuaiYi
//
//  Created by  chenhanmao on 2016/11/16.
//  Copyright © 2016年 30-陈辉华. All rights reserved.
//

#import "DHGategoryView.h"
#import <Masonry.h>

@interface DHGategoryView ()

/**  线条视图 */
@property(nonatomic,weak) UIView *lineView;

/**  分类按钮  */
@property(nonatomic,weak) UIButton *orderFoodButton;

/**   */
@property(nonatomic,strong) NSMutableArray <UIButton *> *buttons;

@end

@implementation DHGategoryView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

-(void)awakeFromNib{
    
    [super awakeFromNib];
    
    [self setupUI];
}


#pragma mark - 搭建界面
- (void)setupUI{
    
    /*------------------添加按钮------------------*/
    
    UIColor *normalColor = [UIColor blackColor];
    UIColor *selectedColor = [UIColor globalColor];
    NSArray *buttonNames = @[@"接诊条件",@"医生简介",@"预约时间"];
    
    NSMutableArray <UIButton *> *buttons = [NSMutableArray array];
    
    NSInteger count = buttonNames.count;
    NSInteger fontSize = 15;
    for (NSInteger i = 0; i < count; i++) {
        
        UIButton *btn = [[UIButton alloc] init];
        
        btn.tag = i;
        
        [btn setTitle:buttonNames[i] forState:UIControlStateNormal];
        
        btn.titleLabel.font = [UIFont systemFontOfSize:fontSize];
        
        [btn setTitleColor:normalColor forState:UIControlStateNormal];
        
        [btn setBackgroundImage:[UIImage imageNamed:@"link_button_02"] forState:UIControlStateNormal];
        
        [btn setTitleColor:selectedColor forState:UIControlStateSelected];
        
        [self addSubview:btn];
        
        [btn addTarget:self action:@selector(clickCategoryButton:) forControlEvents:UIControlEventTouchUpInside];
        
        [buttons addObject:btn];
    }
    
    _buttons = buttons;
    
    buttons[0].selected = YES;
    
    //按钮布局
    
    [buttons[0] mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.top.equalTo(self);
        make.left.height.mas_equalTo(40);
    }];
    
    [buttons[1] mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(buttons[0].mas_right);
        make.top.height.width.equalTo(buttons[0]);
    }];
    
    [buttons[2] mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(buttons[1].mas_right);
        make.top.height.width.equalTo(buttons[1]);
        make.right.equalTo(self);
    }];
    
    
    /*------------------添加线视图------------------*/
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor globalColor];
    [self addSubview: lineView];
    
    _lineView = lineView;
    
    //    UIButton *firstButton = buttons[0];
    _orderFoodButton = buttons[0];
    
    //布局
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.centerX.equalTo(_orderFoodButton);
        make.bottom.equalTo(self);
        make.height.mas_equalTo(2);
    }];
    
}


-(void)didSelectIndex:(NSInteger)index{
    
    [self.lineView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        //移动线条视图
        make.centerX.equalTo(self.orderFoodButton).offset(index * self.lineView.bounds.size.width);
    }];
    
    [UIView animateWithDuration:0.25 animations:^{
        
        [self layoutIfNeeded];
    }];
}


#pragma mark - 按钮响应事件
- (void)clickCategoryButton:(UIButton *) sender {
    
    for (UIButton *btn in _buttons) {
        
        btn.selected = NO;
    }
    
    sender.selected = YES;
    
    //触发代理事件
    
    if ([self.delegate respondsToSelector:@selector(categoryView:didSelectIndex:)]) {
        
        [self.delegate categoryView:self didSelectIndex:sender.tag];
    }
}


- (NSMutableArray<UIButton *> *)buttons {
    
    if (_buttons == nil) {
        _buttons = [NSMutableArray array];
    }
    return _buttons;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
